/**
 * 
 */
package org.gov.vis.data.controller;

import java.util.Date;
import java.util.List;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.data.dao.ProcurementLnDao;
import org.gov.vis.data.dao.ProcurementLnDaoImpl;
import org.gov.vis.data.dto.AIDto;
import org.gov.vis.data.dto.DistributionLnDto;
import org.gov.vis.data.dto.ProcurementLNDto;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author user
 *
 */
public class ProcurementLnCtlrImpl implements ProcurementLnCtlr {

	/**
	 * Method called to Add or Edit the ProcurementLN
	 * @param prLnDto
	 * @throws Exception
	 */

	public void addOrEditPrLN(final ProcurementLNDto _prLnDto) throws Exception {
		try{		
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _prLnCodoErr = ValidatorUtils.validateString(rb.getString("dialog_label_prmt_ln_code"), _prLnDto.getCode(), 3, 20, true);
			if(_prLnCodoErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_ln_code").toUpperCase(), _prLnCodoErr);

			
			String _prLnFrmStnErr = ValidatorUtils.validateString(rb.getString("dialog_label_prmt_ln_frmstation"), _prLnDto.getFrmStation(), 3, 20, true);
			if(_prLnFrmStnErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_ln_frmstation").toUpperCase(), _prLnFrmStnErr);

			String _prLnVhclNoErr = ValidatorUtils.validateString(rb.getString("dialog_label_prmt_ln_vehicleno"), _prLnDto.getVehicleNo(), 4, 50, true);
			if(_prLnVhclNoErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_ln_vehicleno").toUpperCase(), _prLnVhclNoErr);

			/*
			 * Below are NOT required fields but are validated on their data type 
			 */
			String _prLnKGErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prmt_ln_kgs"), _prLnDto.getKgs(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_prLnKGErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_ln_kgs").toUpperCase(), _prLnKGErr);
			
			String _prLnDORErr = ValidatorUtils.validateString(rb.getString("dialog_label_prmt_ln_dtofrcpt"), DU.format(_prLnDto.getDoRpt(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(_prLnDORErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_ln_dtofrcpt").toUpperCase(), _prLnDORErr);

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			/*  Below set of validations check for duplicate records
			 */

		//check if there exists anprLn PrLn Object with the same Code
		//If Code has been duplicated, then throw the alert
			ProcurementLNDto prLnDto = null;
		if(_prLnDto.getPrLnId() > 0){
			//get the PrLnDto on the passed Code name
			prLnDto = this.getPrLnOnCode(_prLnDto.getCode());
			if(prLnDto != null && prLnDto.getPrLnId() != _prLnDto.getPrLnId()){
				errorMsgs.put(rb.getString("dialog_label_prmt_ln_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_PROCUREMENTLN_MSG));
			}
		}//end if
		else{
			//get the PrLnDto on the passed Code name
			prLnDto = this.getPrLnOnCode(_prLnDto.getCode());
			if(prLnDto != null){
				errorMsgs.put(rb.getString("dialog_label_prmt_ln_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_PROCUREMENTLN_MSG));
			}
		}//end else
			 	
			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();

			//add or edit the Procurement LN Object
			_prLnDao.addOrEditProcurementLn(_prLnDto);
		}
		catch(InvalidInputException ie){
			throw ie;
		}
		catch (Exception ex) {
			if(_prLnDto.getPrLnId() > 0){
				throw new SystemException(rb.getString(GuiConstants.DATA_EDIT_PROCUREMENTLN_EXCEPTION));
			}
			else{
				throw new SystemException(rb.getString(GuiConstants.DATA_ADD_PROCUREMENTLN_EXCEPTION));
			}
		}
	}//end addOrEditPrLN

	/**
	 * Method called to delete the ProcurementLN Object based on the passed prSemen Id
	 * @param _prLnId
	 * @throws Exception
	 */

	public void deletePrLN(final long _prLnId) throws Exception {
		try{
			final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();
			_prLnDao.deleteProcurementLn(_prLnId);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_DELETE_PROCUREMENTLN_EXCEPTION));
		}
	}//end deletePrLN

	/**
	 * Method called to get the ProcurementLN based on the passed primary key Id
	 * @param _prLnId
	 * @return
	 * @throws Exception
	 */

	public ProcurementLNDto getPrLN(final long _prLnId) throws Exception {
		try{
			final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();
			ProcurementLNDto _prLnDto = new ProcurementLNDto();
			_prLnDto.setPrLnId(_prLnId);

			ProcurementLNDto prLnDto = null;
			List<ProcurementLNDto> _prSemenList = _prLnDao.getProcurementLns(_prLnDto);
			if(_prSemenList != null && !_prSemenList.isEmpty()){
				prLnDto = _prSemenList.get(0);
			}

			return prLnDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_EDIT_PROCUREMENTLN_EXCEPTION));
		}
	}//end getPrLN


	/**
	 * Method called to get the ProcurementLN based on the passed ReceptDate
	 * @param _prLnRcptDt
	 * @return
	 * @throws Exception
	 */

	public ProcurementLNDto getPrLNOnDtOnRcpt(final Date _prLnRcptDt) throws Exception {
		final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();
		ProcurementLNDto _prLnDto = new ProcurementLNDto();
		_prLnDto.setDoRpt(_prLnRcptDt);

		ProcurementLNDto prLnDto = null;
		List<ProcurementLNDto> _prSemenList = _prLnDao.getProcurementLns(_prLnDto);
		if(_prSemenList != null && !_prSemenList.isEmpty()){
			prLnDto = _prSemenList.get(0);
		}

		return prLnDto;
	}//end getPrLNOnDtOnRcpt

	/**
	 * Method called to get the ProcurementLN based on the passed VehicleNo
	 * @param _prLnVhclNo
	 * @return
	 * @throws Exception
	 */

	public ProcurementLNDto getPrLNOnVehcleNo(final String _prLnVhclNo) throws Exception {
		final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();
		ProcurementLNDto _prLnDto = new ProcurementLNDto();
		_prLnDto.setVehicleNo(_prLnVhclNo);

		ProcurementLNDto prLnDto = null;
		List<ProcurementLNDto> _prSemenList = _prLnDao.getProcurementLns(_prLnDto);
		if(_prSemenList != null && !_prSemenList.isEmpty()){
			prLnDto = _prSemenList.get(0);
		}

		return prLnDto;
	}//end getPrLNOnVehcleNo

	/**
	 * Method called to get the ProcurementLN based on the passed FromStaton
	 * @param _prLnFrmStat
	 * @return
	 * @throws Exception
	 */

	public ProcurementLNDto getPrLNOnFrmStat(final String _prLnFrmStat) throws Exception {
		final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();
		ProcurementLNDto _prLnDto = new ProcurementLNDto();
		_prLnDto.setFrmStation(_prLnFrmStat);

		ProcurementLNDto prLnDto = null;
		List<ProcurementLNDto> _prSemenList = _prLnDao.getProcurementLns(_prLnDto);
		if(_prSemenList != null && !_prSemenList.isEmpty()){
			prLnDto = _prSemenList.get(0);
		}

		return prLnDto;
	}//end getPrLNOnFrmStat

	/**
	 * Method called to get the ProcurementLN based on the passed PrLN Object
	 * @param prLnDto
	 * @return
	 * @throws Exception
	 */

	public ProcurementLNDto getPrLN(final ProcurementLNDto _prLnDto) throws Exception {
		final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();

		ProcurementLNDto prLnDto = null;
		List<ProcurementLNDto> _prSemenList = _prLnDao.getProcurementLns(_prLnDto);
		if(_prSemenList != null && !_prSemenList.isEmpty()){
			prLnDto = _prSemenList.get(0);
		}

		return prLnDto;
	}//end getPrLNs

	/**
	 * Method called to get the list of ProcurementLN based on the passed PrLN Object
	 * @param prLnDto
	 * @return
	 * @throws Exception
	 */

	public List<ProcurementLNDto> getPrLNs(final ProcurementLNDto _prLnDto) throws Exception {
		final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();
		return _prLnDao.getProcurementLns(_prLnDto);
	}//end getPrLNs

	/**
	 * Method called to search for the ProcurementLNs based on the passed PrLN Object
	 * @param prLnDto
	 * @return
	 * @throws Exception
	 */

	public List<ProcurementLNDto> searchPrLNs(ProcurementLNDto _prLnDto) throws Exception {
		try{
			final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();
			return _prLnDao.searchProcurementLns(_prLnDto);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_MANTAIN_PROCUREMENTLN_EXCEPTION));
		}
	}//end searchPrLNs

	@Override
	public ProcurementLNDto getPrLNOnDtOnRcpt(String lnRcptDt) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * Method called to get the AI based on the passed Code
	 * @param _aiCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProcurementLNDto getPrLnOnCode(final String _code) throws Exception {
		final ProcurementLnDao _prLnDao = new ProcurementLnDaoImpl();
		
		final ProcurementLNDto _procurementLNDto = new ProcurementLNDto();
		_procurementLNDto.setCode(_code);


		ProcurementLNDto prLnDto = null;
		List<ProcurementLNDto> _prLnList = _prLnDao.getProcurementLns(_procurementLNDto);
		if(_prLnList != null && !_prLnList.isEmpty()){
			prLnDto = _prLnList.get(0);
		}

		return prLnDto;
	}//end getAIOnCode


}//end class
